"""
Created on Sat Aug 24 15:38:04 2013

@author: chico
"""

from libatomism import *
from PySide import QtCore, QtGui
from msWidget import *
import inspect

class popupUnit(QtGui.QWidget):
    
    def __init__(self,data,mainwindows):
        super(popupUnit, self).__init__()
        self.data = data
        self.initUI()
        
    def initUI(self):
        
        self.vbox = QtGui.QVBoxLayout()
        
        self.vbox.addWidget(QtGui.QLabel("Type var"),1,1)
        
        self.listVarType =  QtGui.QComboBox()
        for item in inspect.getmembers(unitTypeVar)[-1][1].values():
            self.listVarType.addItem(str(item))
        self.vbox.addWidget(self.listVarType)
    
        self.listVarValue =  QtGui.QComboBox()
        self.vbox.addWidget(self.listVarValue)
              
        self.listVarType.currentIndexChanged.connect(self.updatelistVarValues)
        
        self.setLayout(self.vbox)
        
    def updatelistVarValues(self):
            
        listUnits = msUnitsManager.listUnitsAvailable(inspect.getmembers(unitTypeVar)[-1][1].values()[self.listVarType.currentIndex()])
        self.listVarValue.clear()
        for j in listUnits:
               self.listVarValue.addItem(j)
              
 
 
class msUnitsManagerWidget(msWidget):
    
    def __init__(self,data,mainwindows):
        super(msUnitsManagerWidget, self).__init__(data, mainwindows)
        
        targets     = [ "Energy","Time","Length","Mass","Angle" ]
        targetsType = [ unitTypeVar.vEnergy, unitTypeVar.vTime,
                        unitTypeVar.vLength, unitTypeVar.vMass, 
                        unitTypeVar.vAngle ]
        
        self.gridbox = QtGui.QGridLayout()
        self.gridbox.setContentsMargins(0,0,0,0)
        self.gridbox.setSpacing(2)
        i=0
        for unit in targets:
            self.gridbox.addWidget( QtGui.QLabel(unit),i+1,1)
            combobox = QtGui.QComboBox()
            listUnits = msUnitsManager.listUnitsAvailable(targetsType[i])
            for j in listUnits:
                combobox.addItem(j)
   
            self.gridbox.addWidget(combobox,i+1,2)
            i = i+1
        
        updateUnitsButton = QtGui.QPushButton("update")
        self.gridbox.addWidget(updateUnitsButton,i+1,2)

        self.vbox.addLayout(self.gridbox)
        
        self.setMinimumHeight(200)
        self.setMaximumHeight(200)
        self.setMinimumWidth(220)
        self.setMaximumWidth(220)      